package in.muton.edushare.panels.userfeedback;

import in.muton.edushare.EduShareSession;

import java.util.Arrays;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.BugReportsDAO;
import com.edushare.common.vo.BugReports;

public class FeedbackFormPanel extends Panel {

	private static final long serialVersionUID = 1608181687812825357L;
	public static final String[] reportType = {"Bug", "Suggestion", "Feature", "Feedback"};
	private BugReports bug;
	private String reportTypeStr;
	
	public FeedbackFormPanel(String id){
		this(id,"Bug");
	}
	
	public FeedbackFormPanel(String id, String reportType) {
		super(id);
		this.reportTypeStr = reportType;
		add(CSSPackageResource.getHeaderContribution(FeedbackFormPanel.class, "FeedbackFormPanel.css"));
		setBug(new BugReports());
		loadPanel();
	}

	public String getReportTypeStr(){
		return reportTypeStr;
	}
	
	public void setReportTypeStr(String t){
		this.reportTypeStr = t;
	}

	private void loadPanel() {
		final FeedbackPanel fbp = new FeedbackPanel("feedback");
		fbp.setOutputMarkupId(true);
		TextField<String> name = new TextField<String>("nameFFP", new PropertyModel<String>(this, "bug.reporterName"));
		TextField<String> email = new TextField<String>("emailFFP", new PropertyModel<String>(this, "bug.reporterEmail"));
		DropDownChoice<String> type = 
			new DropDownChoice<String>("typeFFP", new PropertyModel<String>(this, "reportTypeStr"), Arrays.asList(reportType));
		TextArea<String> desc = new TextArea<String>("descFFP", new PropertyModel<String>(this, "bug.reportDesc"));
		
		Form<Void> form = new Form<Void>("formFFP");
		form.setOutputMarkupId(true);
		
		AjaxButton submitForm = new AjaxButton("submitForm", form) {
			
			private static final long serialVersionUID = -3494835154232825888L;
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {

					if(getBug().getReporterName()!=null && getBug().getReporterName().length()>ApplicationConstants.MAX_TEXT_FIELD_SIZE){
						warn("Name is too long.");
						afterSubmission();
						target.addComponent(fbp);
						return;
					}else if(getBug().getReporterEmail()!=null && getBug().getReporterEmail().length() > ApplicationConstants.MAX_TEXT_FIELD_SIZE){
						warn("email address is too long.");
						afterSubmission();
						target.addComponent(fbp);
						return;
					}else if(getBug().getReportDesc()==null){
						warn("Please fill-in some description.");
						afterSubmission();
						target.addComponent(fbp);
						return;
					}else if(getBug().getReportDesc().length() > ApplicationConstants.MAX_DESC_SIZE){
						warn("Description is too long.");
						afterSubmission();
						target.addComponent(fbp);
						return;
					}
					
					getBug().setReportType(getReportTypeStr());
					new BugReportsDAO(EduShareSession.get().getSessionORMInterface()).createBugReport(getBug());
					info("Feedback has been sent. Thank You.");
					//reset
					setBug(new BugReports());
					afterSubmission();
					target.addComponent(fbp);
					target.addComponent(form);
			}
		};

		add(form);
		form.add(fbp);
		form.add(name);
		form.add(type);
		form.add(email);
		form.add(desc);
		form.add(submitForm);
	}

	/** Override this for after submission stuffs */
	public void afterSubmission(){}
	
	public void setBug(BugReports bug) {
		this.bug = bug;
	}

	public BugReports getBug() {
		return bug;
	}
	
	
}
